﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using System.Linq;
using System.Collections.Generic;
using AdminModule.IndexMarkServiceReference;
using SharedCode;
using System.ComponentModel;
using AdminModule.CommonClasses;
using AdminModule.UserControls.StructView;

namespace AdminModule.ViewModels
{
    public class CommonVM2 : ViewModelBase
    {
        private IndexMarkServiceClient client = new IndexMarkServiceClient();
        private bool loadOnDemand { get; set; }

        #region IDTerm
        private int _IDTerm = 0;
        private int IDTerm
        {
            get
            {
                if (_IDTerm == 0)
                    throw new ArgumentException("IDTerm == 0, необходимо вызвать метод Init()");
                else
                    return _IDTerm;
            }
            set
            {
                this._IDTerm = value;
            }
        }
        #endregion

        public CommonVM2() { }

        public void Init(StructName structName, int IDTerm)
        {
            this._IDTerm = IDTerm;

            switch (structName)
            {
                case StructName.IndexMark:
                    this.InitLoadIndexMarkStruct();
                    break;

                case StructName.University:
                    this.InitLoadUniversityStruct();
                    break;

                case StructName.Gradations:
                    this.InitLoadGradations();
                    break;
            }
        }

        #region Структура университета
        private ObservableCollection<RootTreeView> universityStructRoots = new ObservableCollection<RootTreeView>();
        public ObservableCollection<RootTreeView> UniversityStructRoots
        {
            get { return this.universityStructRoots; }
            set { this.universityStructRoots = value; }
        }

        private ObservableCollection<AdmJob> jobs = new ObservableCollection<AdmJob>();
        public ObservableCollection<AdmJob> AdmJobCollection
        {
            get { return this.jobs; }
            set { this.jobs = value; }
        }

        public void LoadUniversityStruct(int IDTerm, bool WithRespondents, bool loadOnDemand = false)
        {
            this.loadOnDemand = loadOnDemand;

            this.RaiseInitStarted();
            if (loadOnDemand == true)
                client.GetUniversityStructOnDemandAsync(IDTerm, null, WithRespondents);
            else
                client.GetUniversityStructAsync(IDTerm, WithRespondents);
        }

        private void InitLoadUniversityStruct()
        {
            client.GetUniversityStructCompleted += (s, ev) =>
            {
                RootTreeView root = new RootTreeView("Университет");
                root.Childs.LoadWithClear(ev.Result.Cast<DTOBase>());
                universityStructRoots.Clear();
                universityStructRoots.Add(root);

                if(AdmJobCollection.Count == 0)
                    client.GetAdmJobsAsync();
                else
                    this.RaiseInitCompleted();
            };
            client.GetUniversityStructOnDemandCompleted += (s, ev) =>
            {
                RootTreeView root = new RootTreeView("Университет");
                root.Childs.LoadWithClear(ev.Result.Cast<DTOBase>());
                universityStructRoots.Clear();
                universityStructRoots.Add(root);

                client.GetAdmJobsAsync();
            };
            client.GetAdmJobsCompleted += (s, ev) =>
                {
                    jobs.LoadWithClear(ev.Result);
                    this.RaiseInitCompleted();
                };
        }
        #endregion

        #region Структура ИС
        private ObservableCollection<RootTreeView> indexMarkStructRoots = new ObservableCollection<RootTreeView>();
        public ObservableCollection<RootTreeView> IndexMarkStructRoots
        {
            get { return this.indexMarkStructRoots; }
            set { this.indexMarkStructRoots = value; }
        }

        private ObservableCollection<AdmCategory> categories = new ObservableCollection<AdmCategory>();
        public ObservableCollection<AdmCategory> Categories
        {
            get { return this.categories; }
            set { this.categories = value; }
        }

        private ObservableCollection<AdmUnit> units = new ObservableCollection<AdmUnit>();
        public ObservableCollection<AdmUnit> Units
        {
            get { return this.units; }
            set { this.units = value; }
        }

        public void LoadIndexMarkStruct(int IDTerm, bool WithCharacteristics, bool loadOnDemand = false)
        {
            this.loadOnDemand = loadOnDemand;

            this.RaiseInitStarted();
            if (loadOnDemand == true)
                client.GetIndexMarkStructOnDemandBySectionAsync(IDTerm, null, WithCharacteristics);
            else
                client.GetIndexMarkStructAsync(IDTerm, WithCharacteristics);

        }

        private void InitLoadIndexMarkStruct()
        {
            client.GetIndexMarkStructCompleted += (s, ev) =>
                {
                    RootTreeView root = new RootTreeView("Индексная система");
                    root.Childs.LoadWithClear(ev.Result.Cast<DTOBase>());
                    indexMarkStructRoots.Clear();
                    indexMarkStructRoots.Add(root);

                    if(Categories.Count == 0 || Units.Count == 0)
                        client.GetAdmCategoriesByTermAsync(IDTerm);
                    else
                        this.RaiseInitCompleted();
                };
            client.GetIndexMarkStructOnDemandBySectionCompleted += (s, ev) =>
                {
                    RootTreeView root = new RootTreeView("Индексная система");
                    root.Childs.LoadWithClear(ev.Result);
                    indexMarkStructRoots.Clear();
                    indexMarkStructRoots.Add(root);

                    client.GetAdmCategoriesByTermAsync(IDTerm);
                };
            client.GetAdmCategoriesByTermCompleted += (s, ev) =>
                {
                    categories.LoadWithClear(ev.Result);

                    client.GetAdmUnitsAsync();
                };
            client.GetAdmUnitsCompleted += (s, ev) =>
                {
                    units.LoadWithClear(ev.Result);

                    this.RaiseInitCompleted();
                };
        }
        #endregion

        #region Структура градаций
        private ObservableCollection<RootTreeView> gradations = new ObservableCollection<RootTreeView>();
        public ObservableCollection<RootTreeView> Gradations
        {
            get { return this.gradations; }
            set { this.gradations = value; }
        }

        public void LoadGradationsStruct(int IDTerm, bool loadOnDemand = false)
        {
            this.loadOnDemand = loadOnDemand;

            this.RaiseInitStarted();
            if (Units.Count == 0)
            {
                client.GetAdmUnitsAsync();
            }
            else
            {
                if (loadOnDemand == true)
                    client.GetGradationsStructOnDemandAsync(IDTerm, null);
                else
                    client.GetGradationsStructAsync(IDTerm);
            }
        }

        private void InitLoadGradations()
        {
            client.GetAdmUnitsCompleted += (s, ev) =>
            {
                units.Clear();
                units.Add(null);
                units.LoadWithoutClear(ev.Result);

                if (loadOnDemand == true)
                    client.GetGradationsStructOnDemandAsync(IDTerm, null);
                else
                    client.GetGradationsStructAsync(IDTerm);
            };
            client.GetGradationsStructCompleted += (s, ev) =>
            {
                RootTreeView root = new RootTreeView("Градации");
                root.Childs.LoadWithClear(ev.Result.Cast<DTOBase>());
                gradations.Clear();
                gradations.Add(root);

                this.RaiseInitCompleted();
            };
            client.GetGradationsStructOnDemandCompleted += (s, ev) =>
            {
                RootTreeView root = new RootTreeView("Градации");
                root.Childs.LoadWithClear(ev.Result.Cast<DTOBase>());
                gradations.Clear();
                gradations.Add(root);

                this.RaiseInitCompleted();
            };
        }
        #endregion
    }
}
